<?php

/******************************************************
 *					  	 URL					      *
 ******************************************************/

/**
 * Go to a URL
 * @param string $urlto
 * @return void
 * @author Pablo
 * @since 1.0
 */
function forward($urlto= null){
	//Remove all alredy sent headers
	header_remove();
	
	//Get the URL to go
	if(empty($urlto))
		$urlto= blg_get_site_url();
	
	//Redirect
	header("Location: $urlto");
	exit();
}

/**
 * Returns parsed info about the requested URI (Should only used by blgControler)
 * @author Pablo
 * @since 1.0
 */
function blg_get_url_parts($url= NULL){
	if(empty($url))
		$url= $_SERVER[REQUEST_URI];
		
	$result= new StdClass();
	$result->url= $url;
		
	$url= str_replace(blg_get_site_url(true), '', $url); //Must be true!!
	$url_parts= parse_url($url);
	
	$page= array_filter(explode('/', $url_parts['path']));
	$query= array_filter(explode('&', $url_parts['query']));
	
	$querys= array();
	foreach($query as $i => $pairs){
		list($key, $value)= @explode('=', $pairs);
		$querys[$i]= new stdClass();
		$querys[$i]->key= $key;
		$querys[$i]->value= $value; 
	}	
	
	$result->page= $url_parts['path'];
	$result->host= $url_parts['host'];
	$result->query= $url_parts['query'];
	$result->pages= $page;
	$result->queries= $querys;	
	
	if(empty($result->page))
		$result->page= EMPTY_PAGE;
	if($result->host=='localhost')
		$result->host= null;
	
	return $result;
}

/**
 * Clean a URL
 * @param $url
 * @return $url cleaned without the query part
 * @author Pablo
 * @since 1.0
 */
function blg_clean_url($url= null){	
	$url= blg_get_url_parts($url);
	return blg_get_site_url().$url->page;
}

/******************************************************
 *					  	 COLORS					      *
 ******************************************************/

function blg_color($color, $name){
	global $_COLORS;
	return $_COLORS[$color][$name];	
}

/******************************************************
 *					  	  SITE					      *
 ******************************************************/

/**
 * Return site URL
 * @param boolean $relative Returns the relative or absolute URL (default is absolute)
 * @return string The site URL with a trainling slash
 * @author Pablo
 * @since 1.0
 */
function blg_get_site_url($relative= FALSE){
	global $_CONFIG;	
		
	if($relative!==FALSE)
		return add_trainling_slash(blg_get_config('site_url_relative'));
	else
		return add_trainling_slash(blg_get_config('site_url'));
}

/**
 * Return site PATH
 * @return string The site PATH with a trainling slash
 * @author Pablo
 * @since 1.0
 */
function blg_get_site_path(){
	global $_CONFIG;	
	return add_trainling_slash(blg_get_config('site_path'), DS);
}

/**
 * Returns logical user files directory
 * @param BlgUser $user_entity
 * @return String URL
 * @author Pablo
 * @since 1.0
 */
function blg_get_user_files_url($user_entity=NULL){
	if(empty($user_entity))
		$user_entity= blg_get_logged_in_user_entity();
	
	if(FALSE!==$user_entity){
		return blg_get_site_url().'files/'.$user_entity->code.'/';				
	}else{
		return blg_get_site_url().'files/public/';		
	}	
}

/**
 * Returns fisical user files directory
 * @param BlgUser $user_entity
 * @return String URL
 * @author Pablo
 * @since 1.0
 */
function blg_get_user_files_path($user_entity=NULL){	
	if(empty($user_entity))
		$user_entity= blg_get_logged_in_user_entity();	
	
	if(FALSE!==$user_entity){
		return add_trainling_slash(blg_get_config('data_dir'),DS).$user_entity->guid.DS.'files'.DS; 				
	}else{
		return add_trainling_slash(blg_get_config('data_dir'),DS).'public'.DS.'files'.DS;		
	}	
}

/******************************************************
 *		MODS, PAGES, ACTIONS, VIEWS AND STYLE		  *
 ******************************************************/

/**
 * Generic register function, requires a <i>register.php</i> file in $path
 * @param string $path
 * @return boolean
 * @author Pablo
 * @since 1.0 
 */
function blg_register($path){
	global $_VIEWS, $_COLORS;
			
	$file= add_trainling_slash($path, DS).'register.php';
	if(is_file($file)){
		require_once($file);
		return TRUE;
	}
	
	return FALSE;		
}

/**
 * Generic start function, requires a <i>start.php</i> file in $path
 * @param string $path
 * @return boolean
 * @author Pablo
 * @since 1.0 
 */
function blg_start($path){
	global $_VIEWS, $_COLORS;
			
	$file= add_trainling_slash($path, DS).'start.php';
	if(is_file($file)){
		require_once($file);
		return TRUE;
	}
	
	return FALSE;		
}

/**
 * Require once all PHP files located in a path
 * @param string $path A directory path
 * @return boolean
 * @author Pablo
 * @since 1.0
 */
function require_once_all_php_files($path){
	if(is_dir($path)){	
		foreach (glob($path.'*.php') as $filename){
		    require_once $filename;
		}
		return TRUE;
	}
	return FALSE;	
}

/**
 * Load classes
 * @param string $path A directory path
 * @return boolean
 * @author Pablo
 * @since 1.0
 */
function blg_load_class($path){
	$path= add_trainling_slash($path, DS).'classes'.DS;
	return require_once_all_php_files($path);		
}

/**
 * Load library
 * @param string $path A directory path
 * @return boolean
 * @author Pablo
 * @since 1.0
 */
function blg_load_lib($path){
	$path= add_trainling_slash($path, DS).'lib'.DS;
	return require_once_all_php_files($path);		
}

/**
 * Register all views
 * @return boolean
 * @author Pablo
 * @since 1.0 
 */
function blg_register_views(){
	global $_VIEWS, $_COLORS;
	$path= blg_get_site_path().'views'.DS.blg_get_config('site_view');		
	return blg_register($path);	
}

/**
 * Register all actions
 * @return boolean
 * @author Pablo
 * @since 1.0 
 */
function blg_register_actions(){
	global $_VIEWS, $_COLORS;
	$path= blg_get_site_path().'actions';		
	return blg_register($path);	
}

/**
 * Register all pages
 * @return boolean
 * @author Pablo
 * @since 1.0 
 */
function blg_register_pages(){
	global $_VIEWS, $_COLORS;
	$path= blg_get_site_path().'pages';		
	return blg_register($path);			
}

/**
 * Register a mod
 * @param string $mod_folder
 * @return boolean
 * @author Pablo
 * @since 1.0 
 */
function blg_register_mod($mod_folder){
	global $_VIEWS, $_COLORS;
	$path= blg_get_site_path().'mod'.DS.$mod_folder;		
	return blg_start($path);		
}

/**
 * Get a registed view
 * @param $view_name
 * @param $vars
 * @param $_CONTEXT (HTML|JS|CSS|ACCESS)
 * @return bool This function returns true if a session was successfully started, otherwise false
 * @author Pablo
 * @since 1.0
 */
function blg_view($view_name, $vars= null, $_CONTEXT= CONTEXT_HTML){
	global $_VIEWS, $_COLORS, $_CSS_STACK, $_JS_STACK, $_JS_CODE, $_MENU;
	
	$path= $_VIEWS->path($view_name);
	
	if(!empty($path)){
		ob_start();
		require($path);	
		$return = ob_get_contents();
		ob_end_clean();
		return $return;
	}
	
	return null;
}

/**
 * Load a registed action
 * @author Pablo
 * @since 1.0
 */
function blg_action($action_name, $vars= null, $_CONTEXT= CONTEXT_ACTION){
	global $_VIEWS, $_COLORS, $_CSS_STACK, $_JS_STACK, $_JS_CODE;
	
	$path= $_VIEWS->path($action_name);
	
	if(!empty($path)){
		require($path);	
	}
}

/******************************************************
 *						ACCESS						  *
 ******************************************************/

/**
 * Check access for a page or an action
 * @param $url
 * @return TRUE|string TRUE if access is allowed, or an url string to forward in other case
 * @author Pablo
 * @since 1.0
 */
function blg_check_access($page){
	global $_VIEWS;
	
	$path= $_VIEWS->path($page);
	unset($_ACCESS);	
	
	if(!empty($path) && is_file($path)){
		$_CONTEXT= CONTEXT_ACCESS;
		require($path);
		if(isset($_ACCESS)){
			if($_ACCESS===FALSE)
				$_ACCESS= blg_get_config('url_error_403');
			return $_ACCESS; //Concret access
		}
	}
	
	return TRUE; //Public access			
}

/******************************************************
 *						INPUT						  *
 ******************************************************/

/**
 * Get an input value, searches in $_POST, $_GET, $_SESSION
 * @param $name		Input name
 * @param $default	If not found return a default value
 * @author Pablo
 * @since 1.0
 */
function blg_get_input($name, $default=NULL){
	if(isset($_POST[$name]))
		return filter_input(INPUT_POST, $name);	
	
	if(isset($_GET[$name]))
		return filter_input(INPUT_GET, $name);
		
	if(isset($_SESSION[$name]))
		return $_SESSION[$name];

	return $default;
}

/**
 * Set an input value
 * @param $name		Input name
 * @param $value	Input value
 * @author Pablo
 * @since 1.0
 */
function blg_set_input($name, $value){
	blg_session_set($name, $value, TRUE);
}

/******************************************************
 *					   PHP FUNCTIONS				   *
 ******************************************************/

function blg_extract($haystack, $needle_key, $default=NULL){
	if(array_key_exists($needle_key, $haystack)){
		return $haystack[$needle_key];
	}
	else{
		return $default;
	}	
}